package com.web.selenium;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

import com.web.model.BrowserFactory;
import com.web.model.BrowserType;
import com.web.model.PageBrowser;

public class SeleniumFactory implements BrowserFactory{

	private BrowserType defaultBrowserType;
	
	private int maxSecondsToWait;
	
	public SeleniumFactory(BrowserType defaultBrowserType, int maxSecondsToWait){
		
		this.defaultBrowserType = defaultBrowserType;
		
		this.maxSecondsToWait = maxSecondsToWait;
	}
	
	public PageBrowser newBrowser() {
		
		return newBrowser(defaultBrowserType);
	}

	public PageBrowser newBrowser(BrowserType browserType) {
	
		WebDriver driver = getDriverInstance(browserType);
		PageBrowser browser = new SeleniumWebBrowser(driver, maxSecondsToWait);
		
		return browser;
	
	}
	
	private WebDriver getDriverInstance(BrowserType browserType) {
		
		WebDriver driver = null;
		
		if(browserType == BrowserType.FIREFOX){
			
			driver = new FirefoxDriver();
		}
		else if (browserType == BrowserType.INTERNET_EXPLORER){
			
			driver = new InternetExplorerDriver();
		}
		else if (browserType == BrowserType.CHROME){
			
			driver = new ChromeDriver();
		}
		else if (browserType == BrowserType.HTML_UNIT){
			
			driver = new HtmlUnitDriver();
		}
		else
			throw new IllegalStateException("Browser type is not supported");
		
		return driver;
	}
}
